#!/bin/sh

PID_FILE=/var/run/gateway.pid
OTA_FILE=/root/gateway.update
EXE_FILE=/usr/bin/gateway

start() {
    #1  检查是否有ota更新下来的新程序
    #2 如果有
    #2.1 把原程序删除或者重命名
    #2.2 把新程序mv到原程序的位置
    #2.3 补充执行权限

    if [ -f $OTA_FILE ]
    then 
        #2.1 把原程序删除或者重命名
        rm -f $EXE_FILE
        #2.2 把新程序mv到原程序的位置
        mv -f $OTA_FILE  $EXE_FILE
        #2.3 补充执行权限
        chmod +x $EXE_FILE
    fi
	 
    #3  启动程序
	start-stop-daemon -S -q -p $PID_FILE \
		--exec $EXE_FILE -- daemon
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
stop() {
	printf "Stopping gateway sshd: "
	start-stop-daemon -K -q -p $PID_FILE
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
  	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
